<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>

	<input type="checkbox" checked>
	<input type="radio" checked="checked">

	<hr>

	<table border="1">
		<tr>
			<th><input id="all" type="checkbox"></th>
			<th>姓名</th>
			<th>年龄</th>
			<th>电话</th>
		</tr>
		<tr>
			<td><input id="c1" name="chk" type="checkbox"></td>
			<td>aaa</td>
			<td>bbb</td>
			<td>ccc</td>
		</tr>
		<tr>
			<td><input id="c2" name="chk" type="checkbox"></td>
			<td>aaa</td>
			<td>bbb</td>
			<td>ccc</td>
		</tr>
		<tr>
			<td><input id="c3" name="chk" type="checkbox"></td>
			<td>aaa</td>
			<td>bbb</td>
			<td>ccc</td>
		</tr>
		<tr>
			<td><input id="c4" name="chk" type="checkbox"></td>
			<td>aaa</td>
			<td>bbb</td>
			<td>ccc</td>
		</tr>
		<tr>
			<td><input id="c5" name="chk" type="checkbox"></td>
			<td>aaa</td>
			<td>bbb</td>
			<td>ccc</td>
		</tr>
	</table>

	<script>
		function initCheckBox() {
			var all = document.getElementById('all'),
				chks = document.getElementsByName('chk'),
				len = chks.length,
				j,
				k;

			// console.log(chks)

			all.onclick = function() {
				var i, status = '', bgColor = '';
				// this.checked && (status = 'checked');
				if (this.checked) {
					status = 'checked';
					bgColor = 'yellow';
				} else {
					status = '';
					bgColor = '#fff'
				}
				for (i=0; i<len; i++) {
					chks[i].checked = status;
					chks[i].parentNode.parentNode.style.backgroundColor = bgColor;
				}
			};

			for (j=0; j<len; j++) {
				chks[j].onclick = function() {
					// TODO
					// console.log(this);
					if (this.checked) {
						this.parentNode.parentNode.style.backgroundColor = 'yellow';
					} else {
						this.parentNode.parentNode.style.backgroundColor = '#fff';
					}
					
					if (isCheckedAll()) {
						all.checked = 'checked';
					} else {
						all.checked = '';
					}
				}
			}

			function isCheckedAll() {
				for (k=0; k<len; k++) {
					if (!chks[k].checked) {
						return false;
					}
				}
				return true;
			}
		}

		initCheckBox();
	</script>
</body>
</html>